package com.sxj.springsourcecode.springframework.factory.support.strategy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.NoOp;

import java.lang.reflect.Constructor;

/**
 * @ProjectName: spring-source-code
 * @Package: com.sxj.springsourcecode.springframework.factory.support.strategy
 * @ClassName: CglibInstantiationStrategy
 * @Author: sxj
 * @Description:
 * @Date: 2024/2/21 13:55
 * @Version: 1.0.0
 */
public class CglibInstantiationStrategy implements InstantiationStrategy{
    @Override
    public Object instantBean(Class clz, Constructor ctr, Object[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clz);
        enhancer.setCallback(new NoOp() {
            @Override
            public int hashCode() {
                return super.hashCode();
            }
        });
        Object bean;
        if (args == null){
            bean = enhancer.create();
        }else {
            bean = enhancer.create(ctr.getParameterTypes(),args);
        }
        return bean;
    }
}
